package com.cd.cms.modules.mid.service;

import java.util.Date;
import java.util.List;

import com.cd.cms.modules.mid.entity.PlatformInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cd.cms.common.persistence.Page;
import com.cd.cms.common.service.CrudService;
import com.cd.cms.modules.mid.entity.GoodInfo;
import com.cd.cms.modules.mid.dao.GoodInfoDao;
import com.cd.cms.modules.mid.dao.PlatformInfoDao;
import com.cd.cms.modules.sys.entity.Dict;

/**
 * 产品Service
 * @author XueD
 * @version 2016-05-10
 */
@Service
@Transactional(readOnly = true)
public class GoodInfoService extends CrudService<GoodInfoDao, GoodInfo> {

	@Autowired
	private PlatformInfoService platformInfoService;
	@Autowired
	private PlatformInfoDao platformInfoDao;
	public GoodInfo get(String id) {
		return super.get(id);
	}
	
	public List<GoodInfo> findList(GoodInfo goodInfo) {
		return super.findList(goodInfo);
	}
	
	public Page<GoodInfo> findPage(Page<GoodInfo> page, GoodInfo goodInfo) {
		return super.findPage(page, goodInfo);
	}
	
	@Transactional(readOnly = false)
	public void save(GoodInfo goodInfo) {
		super.save(goodInfo);

	}
	
	@Transactional(readOnly = false)
	public void delete(GoodInfo goodInfo) {
		super.delete(goodInfo);
	}
	@Transactional
	public void updatePlatform(GoodInfo goodInfo){
		dao.updatePlatform(goodInfo);
		dao.update(goodInfo);
	}
	
	@Transactional
	public void updatePrice(GoodInfo goodInfo,PlatformInfo platformInfo){
		platformInfoDao.update(platformInfo);
		dao.update(goodInfo);
	}
	@Transactional
	public void inserPlatformInfo(GoodInfo goodInfo){
		dao.insertPlatformInfo(goodInfo);
	}
	@Transactional
	public  void insetAll(List<GoodInfo> goodInfo, String platformType, List<Dict> dictlist, List<GoodInfo> goodInfos, List<PlatformInfo> platformInfos){
		Date d = new Date();
		for(GoodInfo goodinfo:goodInfo){
			goodinfo.setCreateDate(d);
			goodinfo.setUpdateDate(d);
			GoodInfo returnGoodInfo = existGoodInfo(goodinfo,goodInfos);
			if(returnGoodInfo!=null){
				goodinfo.setGoodInfoId(returnGoodInfo.getGoodInfoId());
				dao.update(goodinfo);
			}else{
				goodinfo.setIsvalid("1");
				dao.insert(goodinfo);
			}
			//判断platformType平台号
			String [] platformarray = platformType.split(",");
            if(platformarray[0].equals("1")){
            	for(Dict dicts:dictlist){
            		if(dicts.getValue().equals("1")){
            			continue;
            		}
					goodinfo.setPlatformType(dicts.getValue());
					PlatformInfo platformInfo = existPlatformInfo(goodinfo,platformInfos);
					if(platformInfo!=null){
						goodinfo.setpId(platformInfo.getpId());
						this.updatePlatform(goodinfo);
					}else{
						goodinfo.setIsvalid("1");
						this.inserPlatformInfo(goodinfo);
					}
            	}
            }else{
            	for(String platfor:platformarray){
            		goodinfo.setPlatformType(platfor);
					PlatformInfo platformInfo = existPlatformInfo(goodinfo,platformInfos);
					if(platformInfo!=null){
						goodinfo.setpId(platformInfo.getpId());
						this.updatePlatform(goodinfo);
					}else{
						goodinfo.setIsvalid("1");
						this.inserPlatformInfo(goodinfo);
					}
            	}
            }
		}
	}

	private GoodInfo existGoodInfo(GoodInfo newGoodInfo,List<GoodInfo> list){
		if(list==null){
			return null;
		}
		if(list.size()==0){
			return null;
		}
		for(GoodInfo good:list){
			if(good.getgEngDesc().equals(newGoodInfo.getgEngDesc())){
				return good;
			}
		}
		return null;
	}

	private PlatformInfo existPlatformInfo(GoodInfo newGoodInfo,List<PlatformInfo> list){
		if(list==null){
			return null;
		}
		if(list.size()==0){
			return null;
		}
			for(PlatformInfo platformInfo:list){
				if((platformInfo.getPlatformType().equals(newGoodInfo.getPlatformType()))&&(platformInfo.getgEngDesc().equals(newGoodInfo.getgEngDesc()))){
					platformInfo.setnPrice(newGoodInfo.getnPrice());
					platformInfo.setnSellPrice(newGoodInfo.getPurchasingPrice());
					return platformInfo;
				}
			}
		return null;
	}
}